//编写程序，用命令行参数实现比较两个文件是否长度和内容完全相同，输出比较结果。（用Yes和No表示）

import java.io.*;
import java.lang.*;

public class Demo02 {
    private String file1 = null;  //用来存储对比的两个文件名
    private String file2 = null;

    //有参构造函数
    public Demo02(String file1, String file2) {
        this.file1 = file1;
        this.file2 = file2;
    }

    //文件内容比对函数
    private void CmpFile(String file1, String file2) {
        boolean flag = false; //判断字节是否相同
        try {
            //FileInputStream以字节流的形式打开文件
            BufferedInputStream inFile1 = new BufferedInputStream(new FileInputStream(file1));
            BufferedInputStream inFile2 = new BufferedInputStream(new FileInputStream(file2));
            if (inFile1.available() == inFile2.available()) { //字节数相等
                int a1 = inFile1.read(); //读取第一个文件内容
                int a2 = inFile2.read(); //读取第二个文件内容
                while (a1 != -1 && a2 != -1) { //如果没有读取到文件结尾处
                    if (a1 != a2) { //如果字节不同
                        System.out.println("No");
                        flag = true;
                        break;
                    }
                    a1 = inFile1.read();
                    a2 = inFile2.read();
                }
                if (!flag) {
                    System.out.println("Yes");
                }
            } else { //字节数不等
                System.out.println("No");
            }
            inFile1.close(); //关闭1流
            inFile2.close(); //关闭2流
            System.exit(0);
        } catch (IOException error) {
            error.printStackTrace();
        }
    }

    //获取输入的文件名
    private static String inputFileName() {
        String fileName = null;
        //System.in是Stream，InputStreamReader把字节流格式化为字符流读取，以字符流的形式读取输入缓冲区
        BufferedReader buffRead = new BufferedReader(new InputStreamReader(System.in));
        try {
            fileName = buffRead.readLine();  //得到字符串形式的文件路径
        } catch (IOException error) {   //抛出异常
            error.printStackTrace();
        }
        return fileName;
    }

    public static void main(String[] args) {
        System.out.println("请分别输入两个文件的路径名");
        System.out.print("File1:");
        String file1 = inputFileName();
        System.out.print("File2:");
        String file2 = inputFileName();
        Demo02 fileCompare = new Demo02(file1, file2);
        fileCompare.CmpFile(file1, file2); //调用比较函数
    }
}
